package com.sample.qr;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;

import com.google.zxing.client.android.Intents;
import com.loyal.qr.ui.CaptureActivity;
import com.loyal.qr.ui.LandActivity;
import com.loyal.qr.ui.PortActivity;
import com.loyal.qr.zbar.BarCodeView;

public class SplashActivity extends BaseActivity {
    private ActivityResultLauncher<Intent> scanLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ActivityResultLauncher<String[]> permissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(),
                result -> {
                    if (result.containsValue(false)) {
                        finish();
                    }
                });
        scanLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                it -> {
                    if (RESULT_OK != it.getResultCode())
                        return;
                    Intent data = it.getData();
                    String result = null == data ? "" : data.getStringExtra(Intents.Scan.RESULT);
                    showToast(TextUtils.isEmpty(result) ? "未扫描到！！" : result);
                });

        permissionLauncher.launch(new String[]{Manifest.permission.CAMERA});
        BarCodeView barCodeView = findViewById(R.id.barCode);
        barCodeView.setBarCode("qwer1234");
        barCodeView.setTextColor(Color.BLUE);
        barCodeView.setBoldText(true);

        findViewById(R.id.btn_size).setOnClickListener(v -> {
            barCodeView.setTextSize(20f);
        });
        findViewById(R.id.btn_bold).setOnClickListener(v -> {
            barCodeView.setBoldText(false);
        });
        findViewById(R.id.btn_color).setOnClickListener(v -> {
            barCodeView.setTextColor(Color.BLACK);
        });
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        findViewById(R.id.hView).setOnClickListener(v -> {
            Intent intent = new Intent(this, LandActivity.class);
            scanLauncher.launch(intent);
        });
        findViewById(R.id.pView).setOnClickListener(v -> {
            Intent intent = new Intent(this, PortActivity.class);
            scanLauncher.launch(intent);
        });
        findViewById(R.id.CaptureActivity).setOnClickListener(v -> {
            Intent intent = new Intent(this, CaptureActivity.class);
            scanLauncher.launch(intent);
        });
    }
}
